package com.jspgou.common.web;

import javax.servlet.http.*;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;

/**
 * Cookie 辅助类
 * 
 * @author liufang
 * 
 */
public class CookieUtils {
	/**
	 * 每页条数cookie名称
	 */
	public static final String COOKIE_PAGE_SIZE = "_cookie_page_size";
	/**
	 * 默认每页条数
	 */
	public static final int DEFAULT_SIZE = 20;
	/**
	 * 最大每页条数
	 */
	public static final int MAX_SIZE = 200;

	/**
	 * 获得cookie的每页条数
	 * 
	 * 使用_cookie_page_size作为cookie name
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return default:20 max:200
	 */
	public static int getPageSize(HttpServletRequest request) {
        Assert.notNull(request);
        Cookie cookie = getCookie(request,COOKIE_PAGE_SIZE);
        int count = 0;
        if(cookie != null){
			try {
				count = Integer.parseInt(cookie.getValue());
			} catch (Exception e) {
			}
        }
		if (count <= 0) {
				count = DEFAULT_SIZE;
		} else if (count > MAX_SIZE) {
				count = MAX_SIZE;
		}
        return count;
    }

	/**
	 * 获得cookie
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param name
	 *            cookie name
	 * @return if exist return cookie, else return null.
	 */
    public static Cookie getCookie(HttpServletRequest request, String name){
        Assert.notNull(request);
        Cookie[] cookies = request.getCookies();
		if (cookies != null && cookies.length > 0) {
			for (Cookie c : cookies) {
				if (c.getName().equals(name)) {
					return c;
				}
			}
		}
		return null;
    }

	/**
	 * 取消cookie
	 * 
	 * @param response
	 * @param name
	 * @param domain
	 */
    public static void cancleCookie(HttpServletResponse response, String name, 
    		String domain){
        Cookie cookie = new Cookie(name, null);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        if(!StringUtils.isBlank(domain)){
            cookie.setDomain(domain);
        }
        response.addCookie(cookie);
    }
}
